package com.polaris.live.ui.main.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.polaris.live.R
import com.polaris.live.adapter.home.CommunityRecommendAdapter
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentHotWomanBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.interfaces.OnAutoRefreshListener
import com.polaris.live.utils.interfaces.OnRefreshStateListener
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.viewmodel.home.HotViewModel

/**
 * 热门新人-女
 *
 * @author Created by lucas on 2023/10/17 15:57
 * @since 1.0.0
 */
class HotWomanNewcomerFragment : AbstractHotListFragment<HotViewModel, FragmentHotWomanBinding>(),
    OnAutoRefreshListener, OnRefreshStateListener {

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy { LinearLayoutManager(context) }

    override val mAdapter by lazy { CommunityRecommendAdapter() }

    override val listLiveData by lazy { mViewModel.listLiveData }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        NavigationBarUtils.setImmersionBarForMain(this, mBinding.viewPleaseHolder)
        mViewModel.pageType = HotViewModel.NEW_LIST
        mAdapter.setNeedShowMedal(true)
    }

    override fun initListener() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = mAdapter.getItemOrNull(position) as? DisplayUserBean ?: return@setOnItemChildClickListener
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                when (view.id) {
                    R.id.iv_say_hi -> {
                        Router.builder(RoutePath.MESSAGE_ACTIVITY)
                            .withInt("page", UserConst.From.HOT)
                            .withString("nickname", item.base.nickname)
                            .withString("avatar", item.base.avatar)
                            .withLong("userId", item.base.userId)
                            .navigation()
                    }

                    R.id.av_head -> {
                        val state = UserStateUtils.resolveUserState(item.base)
                        if (state == UserStateUtils.USER_STATE_OFFLINE) {
                            Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                                .withInt("page", UserConst.From.HOT)
                                .withLong("userId", item.base.userId)
                                .withAny("userBasic", item.base)
                                .navigation(requireActivity())
                        } else {
                            LiveUtils.invokeSpectator(requireContext(), item, needGoProfile = true)
                        }
                    }
                }
            })
        }
    }

    override fun onRefreshState() {
        if (!isCreated()) return
    }

}